home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / MANUALS / WWW / LIST.C next >
C/C++ Source or Header  |  1998-05-07  |  15KB  |  534 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /*  Program : to create various index files in both txt and html format    */
  4. /*                                                                         */
  5. /*                                                                         */
  6. /*  Author  : C.Carpenter                                                  */
  7. /*  Date    : 18/04/98                                                     */
  8. /*                                                                         */
  9. /***************************************************************************/
  10.  
  11.  
  12.  
  13. /***************************************************************************/
  14. /*  includes                                                               */
  15. /***************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20.  
  21.  
  22. /***************************************************************************/
  23. /*  defines                                                                */
  24. /***************************************************************************/
  25. #define FALSE          0
  26. #define TRUE           1
  27. #define MAX_FILENAME   256
  28. #define MAX_EXCLUDES   50
  29. #define MAX_FILES      1000
  30.  
  31. /*#ifdef __riscos
  32. #define strcasecmp stricmp
  33. #endif
  34.  */
  35.  
  36.  
  37. /***************************************************************************/
  38. /*  global variables                                                       */
  39. /***************************************************************************/
  40. typedef struct 
  41. {  
  42.   char filename[MAX_FILENAME]; 
  43. } exclude_rec;
  44.  
  45. exclude_rec excludes[MAX_EXCLUDES];
  46. int total_excludes;
  47.  
  48. typedef struct
  49. {
  50.   char rights[11];
  51.   char val[2];
  52.   char user[10];
  53.   char group[10];
  54.   char size[10];
  55.   char dayword[4];
  56.   char month[4];
  57.   char daynum[3];
  58.   char hour[3];
  59.   char min[3];
  60.   char sec[3];
  61.   char year[5];
  62.   char filename[MAX_FILENAME];
  63.   char directory[MAX_FILENAME];
  64.   
  65. } rec;
  66.  
  67. rec record[MAX_FILES];
  68. int indx[MAX_FILES];
  69. int total_files;
  70.  
  71. char d[20];
  72.  
  73.  
  74.  
  75.  
  76. /***************************************************************************/
  77. /*  function names                                                         */
  78. /***************************************************************************/
  79. int readData(char *filename);
  80. int readExcludes(void);
  81. int checkExcludes(char *filename);
  82. int createAlphaList(char *fname);
  83. int createAuthorList(char *fname);
  84. int createLatestChanges(char *filename);
  85. int writeHTMLHeader(char *filename, char *title, char *header);
  86. int writeHTMLFooter(char *filename);
  87.  
  88.  
  89.  
  90.  
  91.  
  92. /***************************************************************************/
  93. /*  main                                                                   */
  94. /***************************************************************************/
  95. int main(int argc, char *argv[])
  96. {
  97.   
  98.   
  99.   /* Check for the right number of parameters */
  100.   if (argc!=3) 
  101.   {
  102.     printf("Usage: list <filein> <fileout>\n");
  103.     return(1); 
  104.   }   
  105.  
  106.   /* read in the excludes file */
  107.   if ((total_excludes = readExcludes()) <0)
  108.   {
  109.     printf("Unable to open excludes file\n");
  110.     return(1); 
  111.   }
  112.   
  113.   total_files = readData(argv[1]);
  114.   
  115.   if (total_files > 0)
  116.   {
  117.     createAlphaList(argv[2]);
  118.     createAuthorList(argv[2]);
  119. //    createLatestChanges(argv[2]);
  120.   }
  121.  
  122.   return(0);
  123. }
  124.  
  125.  
  126.   
  127.   
  128. /***************************************************************************/
  129. /*  readData                                                               */
  130. /***************************************************************************/  
  131. int readData(char *filename)
  132. {  
  133.   int count=0, k=0;
  134.   char directory[MAX_FILENAME]; 
  135.   FILE *handle; 
  136.   char buffer[256];
  137.   
  138.   
  139.   
  140.   /* Make sure we can open the in file */
  141.   if ((handle=fopen(filename, "r"))==NULL)
  142.   {
  143.     printf("Unable to open file '%s' for reading\n", filename);
  144.     exit(1); 
  145.   }
  146.  
  147.  
  148.   /* Read in the data from the ls -lR list into an array */
  149.   count = 0;
  150.   directory[0]='\0';
  151.   while (!feof(handle))
  152.   {
  153.     /* Read the next line */
  154.     fgets(buffer, sizeof(buffer), handle);
  155.     if ((strrchr(buffer, ':')!=NULL) && (strchr(buffer, ' ')==NULL))
  156.     {
  157.       strncpy(directory, buffer, 256);
  158.       directory[255]='\0';
  159.       directory[strlen(directory)-2]='\0'; // Get rid of the ':'
  160.     }
  161.     else
  162.     {
  163.       if (strlen(buffer)>1)
  164.       {
  165.         sscanf(buffer, "%10s %1d %8s %8s %9s %3s %3s %2s %2s:%2s:%2s %4s %s",
  166.                      record[count].rights,
  167.                      record[count].val,
  168.                      record[count].user,
  169.                      record[count].group,
  170.                      record[count].size,
  171.                      record[count].dayword,
  172.                      record[count].month,
  173.                      record[count].daynum,
  174.                      record[count].hour,
  175.                      record[count].min,
  176.                      record[count].sec,
  177.                      record[count].year,
  178.                      record[count].filename );
  179.      
  180.         strcpy(record[count].directory, directory);
  181.         k=atoi(record[count].size);
  182.         k=k/1024;
  183.         if (k!=0)
  184.         {
  185.           sprintf(record[count].size, "%dk", k);
  186.         }
  187.         
  188.         if (!checkExcludes(record[count].filename))
  189.         {         
  190.           indx[count]=count;
  191.           count ++;
  192.         }
  193.       }
  194.     }    
  195.   }
  196.   fclose(handle);
  197.   return(count);
  198.   
  199.  
  200.  
  201. /***************************************************************************/
  202. /*  createAlphaList                                                        */
  203. /***************************************************************************/
  204. int createAlphaList(char *fname)
  205. {  
  206.   int swapped, speedup, loop, tmp;
  207.   FILE *handle;
  208.   char filename[MAX_FILENAME], buffer[256];
  209.  
  210.   
  211.   /* Sort the list */
  212.   swapped = TRUE;
  213.   speedup = 0;
  214.   while (swapped==TRUE)
  215.   {
  216.     swapped=0;
  217.     for (loop=0; loop<total_files-1-speedup; loop++)
  218.     {
  219.       if (strcasecmp(record[indx[loop]].filename, record[indx[loop+1]].filename) >0 )
  220.       {    
  221.       
  222.         tmp          = indx[loop];
  223.         indx[loop]   = indx[loop+1];
  224.         indx[loop+1] = tmp;
  225.         swapped=TRUE;
  226.       } 
  227.     }
  228.     speedup ++;
  229.   }
  230.   
  231.   
  232.   
  233.   
  234.   /* Write out the sorted list to the file */
  235.     sprintf(filename, "%s-alpha-list.html", fname);
  236. //  strcpy(filename, "alpha/htm");
  237.   
  238.   /* Make sure we can open the html file */
  239.   if ((handle=fopen(filename,"w"))==NULL)
  240.   {
  241.     printf("Unable to open file '%s'\n", filename);
  242.     return(-1); 
  243.   }
  244.   fclose(handle);
  245.   
  246.   
  247.   /* Create the header */
  248.   sprintf(buffer, "%s Index in Alphabetical order of filename", fname);
  249.   writeHTMLHeader(filename, buffer, buffer);
  250.   
  251.   
  252.   
  253.  
  254.  
  255. /*  for (loop=0; loop<count; loop++)
  256.   {
  257.     fprintf(handle, "%-40s %-10s %2s:%2s %2s %3s %4s  %s\n", 
  258.                        record[indx[loop]].filename, 
  259.                        record[indx[loop]].size, 
  260.                        record[indx[loop]].hour,
  261.                        record[indx[loop]].min,
  262.                        record[indx[loop]].daynum,
  263.                        record[indx[loop]].month,
  264.                        record[indx[loop]].year,
  265.                        record[indx[loop]].directory);
  266.   }
  267.   fclose(handle);
  268. */  
  269.   
  270.   
  271.  
  272.   
  273.   
  274.   /* Make sure we can open the html file */
  275.   if ((handle=fopen(filename,"a"))==NULL)
  276.   {
  277.     printf("Unable to open file '%s'\n", filename);
  278.     return(-1); 
  279.   }  
  280.   fprintf(handle, "<table border=0>\n");
  281.   
  282.   
  283.   
  284.   for (loop=0; loop<total_files; loop++)
  285.   {
  286.     sprintf(buffer, "%-25s", record[indx[loop]].filename);
  287.     buffer[20]='\0';
  288.     fprintf(handle, "<tr><td>%-25s<td align=right>%.9s<td>%2s:%2s %2s %3s %4s", 
  289.                     buffer,
  290.                     record[indx[loop]].size, 
  291.                     record[indx[loop]].hour,
  292.                     record[indx[loop]].min,
  293.                     record[indx[loop]].daynum,
  294.                     record[indx[loop]].month,
  295.                     record[indx[loop]].year);
  296.                     
  297.     sprintf(buffer, "%s/%s", 
  298.                     record[indx[loop]].directory, 
  299.                     record[indx[loop]].filename);
  300.     if (strlen(buffer)>27)
  301.       strcpy(buffer+27,"...\0");
  302.                     
  303.     fprintf(handle, "<td><a href=\"%s/%s\">%s</a>",                  
  304.                     record[indx[loop]].directory,
  305.                     record[indx[loop]].filename,
  306.                     buffer);
  307.                     
  308.     fprintf(handle,"\n");        
  309.   }
  310.   
  311.   fprintf(handle, "</table>\n");  
  312.   
  313.   fclose(handle);
  314.   
  315.   writeHTMLFooter(filename);
  316.   
  317.   
  318.   return(0);
  319. }
  320.  
  321.  
  322.  
  323. /***************************************************************************/
  324. /*  createAuthorList                                                       */
  325. /***************************************************************************/
  326. int createAuthorList(char *fname)
  327. {
  328.   char filename[MAX_FILENAME];
  329.   char buffer[256], olddir[256];
  330.   FILE *handle;
  331.   int loop;
  332.   
  333.  
  334.   sprintf(filename, "%s-author-list.html", fname);
  335. //  strcpy(filename, "author/htm");
  336.   
  337.   /* Make sure we can open the html file */
  338.   if ((handle=fopen(filename,"w"))==NULL)
  339.   {
  340.     printf("Unable to open file '%s'\n", filename);
  341.     return(-1); 
  342.   }
  343.   fclose(handle);
  344.   
  345.   
  346.   /* Create the header */
  347.   sprintf(buffer, "%s Index by Author", fname);
  348.   writeHTMLHeader(filename, buffer, buffer);
  349.   
  350.   
  351.   /* Make sure we can open the html file */
  352.   if ((handle=fopen(filename,"a"))==NULL)
  353.   {
  354.     printf("Unable to open file '%s'\n", filename);
  355.     return(-1); 
  356.   } 
  357.   
  358.  
  359.  
  360.   olddir[0]='\0';
  361.   fprintf(handle, "<ul>\n");
  362.   for (loop=0; loop<total_files; loop++)
  363.   {
  364.     if (strcmp(olddir, record[loop].directory) != 0)
  365.     {
  366.       /* new directory name */
  367.       if (loop>0)
  368.       {
  369.         fprintf(handle, "</ul>\n");
  370.         fprintf(handle, "</ul>\n");
  371.       }
  372.       fprintf(handle, "<ul>\n");
  373.       strcpy(olddir, record[loop].directory);
  374.       fprintf(handle, "<li>Directory: <a href=\"%s\">%s</a></li>\n",
  375. record[loop].directory, record[loop].directory); 
  376. fprintf(handle, "<ul>\n");
  377.     }
  378.     
  379.     
  380.     sprintf(buffer, "%-20s", record[indx[loop]].filename);
  381.     buffer[20]='\0';
  382.     fprintf(handle, "<li>%2s:%2s %2s %3s %4s", 
  383.                     record[loop].hour,
  384.                     record[loop].min,
  385.                     record[loop].daynum,
  386.                     record[loop].month,
  387.                     record[loop].year);
  388.                     
  389.     sprintf(buffer, "%s", record[loop].filename);
  390.     if (strlen(buffer)>27)
  391.       strcpy(buffer+27,"...\0");
  392.                     
  393.     fprintf(handle, "<a href=\"%s/%s\">%s</a>(%s)</li>",                  
  394.                     record[loop].directory,
  395.                     record[loop].filename,
  396.                     buffer,
  397.                     record[loop].size);
  398.                     
  399.     fprintf(handle,"\n");        
  400.   }
  401.   
  402.   fprintf(handle, "</ul>\n");  
  403.   fprintf(handle, "</ul>\n");
  404.   fclose(handle);
  405.   
  406.   writeHTMLFooter(filename);
  407.   
  408.   
  409.   return(0); 
  410. }
  411.  
  412.  
  413.  
  414. /***************************************************************************/
  415. /*  createLatestChanges                                                    */
  416. /***************************************************************************/
  417. int createLatestChanges(char *filename)
  418. {
  419.   return(0); 
  420. }
  421.  
  422.  
  423.  
  424. /***************************************************************************/
  425. /*  checkExcludes                                                          */
  426. /***************************************************************************/
  427. int checkExcludes(char *text)
  428. {
  429.   
  430.   int loop;
  431.   
  432.   for (loop=0; loop<total_excludes; loop++)
  433.   {
  434.     if (strstr(text, excludes[loop].filename)!=NULL)
  435.     {
  436. //      printf("%s", text);
  437.       return(TRUE); 
  438.     }
  439.   }
  440.     
  441.   return(FALSE); 
  442. }
  443.  
  444.  
  445.  
  446. /***************************************************************************/
  447. /*  readExcludes                                                           */
  448. /***************************************************************************/
  449. int readExcludes(void)
  450. {
  451.   FILE *handle;
  452.   char tmp[80];
  453.   int count=0;
  454.  
  455.  
  456.   if ((handle = fopen("excludes", "r"))==NULL)
  457.   {
  458.     return(FALSE); 
  459.   }
  460.   
  461.   count=0;
  462.   while (!feof(handle) & (count < MAX_EXCLUDES))
  463.   {
  464.     fgets(tmp, sizeof(tmp), handle);
  465.     if (strlen(tmp)>1)
  466.     {
  467.       tmp[strlen(tmp)-1]='\0';
  468.       strncpy(excludes[count].filename, tmp, sizeof(excludes[count].filename));
  469.       excludes[count].filename[MAX_FILENAME-1]='\0';
  470.       count++;
  471.     } 
  472.   }
  473.   count--;
  474.   total_excludes=count;
  475.  
  476.   fclose(handle);
  477.   return(TRUE);
  478. }
  479.  
  480.  
  481.  
  482. /***************************************************************************/
  483. /*  writeHTMLHeader                                                        */
  484. /***************************************************************************/
  485. int writeHTMLHeader(char *filename, char *title, char *header)
  486. {
  487.   FILE *handle;
  488.   
  489.   if ((handle=fopen(filename, "w")) == NULL)
  490.   {
  491.     printf("Unable to open file '%s' for writing\n", filename);
  492.     return(-1); 
  493.   }
  494.   
  495.   fprintf(handle, "<html><head><title>%s</title></head>\n", title);
  496.   fprintf(handle, "<body background=\"www/images/bg.gif\">\n");
  497.   fprintf(handle, "<h2 align=center>%s</h2>\n", header);
  498.   
  499.   fprintf(handle, "<p>This file is automatically generated each night by this <a href=\"www/list.c\">C source
  500. program.</a>\n");
  501.   fprintf(handle, "<hr>\n");
  502.   fclose(handle);
  503.   return(0);  
  504. }
  505.  
  506.  
  507.  
  508. /***************************************************************************/
  509. /*  wrtieHTMLFooter                                                        */
  510. /***************************************************************************/
  511. int writeHTMLFooter(char *filename)
  512. {
  513.   FILE *handle;
  514.   
  515.   if ((handle=fopen(filename, "a")) == NULL)
  516.   {
  517.     printf("Unable to open file '%s' for appending\n", filename);
  518.     return(-1); 
  519.   }
  520.   
  521.   
  522.   fprintf(handle, "<hr>\n");
  523. //  fprintf(handle, "<p>Automatically generated at %s</p>\n", d);
  524.   fprintf(handle, "<p><a href=\"mailto:ftpadmin@barnet.ac.uk\">ftpadmin@barnet.ac.uk</a></p>\n");
  525.   fprintf(handle, "</body></html>\n");
  526.   
  527.   fclose(handle);
  528.   
  529.   return(0); 
  530. }
  531.  
  532.  
  533.